package org.iogame.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.iogame.data.DataMap;
import org.iogame.socket.IoGameSocket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author lc
 * @since 2022/10/1
 */
@RestController
@Slf4j
@RequestMapping("api")
public class InfoApi {

    @Value("${server.port}")
    private String port;

    @GetMapping("port")
    public Object port(){
        return port;
    }

    @GetMapping("group")
    public Object listGroup(){
        return DataMap.CLASS_INFO_LIST;
    }

    @GetMapping("method")
    public Object listMethod(String key){
        return DataMap.MAP.get(key);
    }

    @GetMapping("connect")
    public void connect(String url,String userId){
        IoGameSocket.connect(url,userId);
    }


    @Data
    static class SendDto{
        private String key;
        private String json;
        private String userId;
    }

    @PostMapping("send")
    public void send(@RequestBody SendDto sendDto){
        log.info("消息对象: {}",sendDto);
        IoGameSocket.send(sendDto.key,sendDto.json,sendDto.userId);
    }
}
